/**
 * http异常
 */
export class HttpException extends Error {
    public message: string;
    public errorCode: number;
    public code: number;
    public data: any;
    public isBuffer = false;
    public responseType: string | undefined;
    /**
     * 
     * @param data 数据
     * @param msg 信息
     * @param errorCode 异常编码
     * @param code 编码
     */
    constructor(data?: unknown, msg = "服务器异常，请联系管理员", errorCode = 10000, code = 400) {
        super();
        this.message = msg;
        this.errorCode = errorCode;
        this.code = code;
        this.data = data
    }

    put(key: string, value: any) {
        this[key] = value;
    }
}

/**
 * Http参数异常
 */
export class ParamException extends HttpException {
    /**
     * 
     * @param msg 错误消息
     * @param errorCode 错误编码
     */
    constructor(code?: number, msg?: string, errorCode?: number) {
        super();
        this.code = code || 422;
        this.message = msg || "参数错误";
        this.errorCode = errorCode || 10000;
    }
}

/**
 * 密码上限异常
 */
export class UserPasswordRetryLimitExceedException extends HttpException {
    public message: string;
    public errorCode: number;
    public code: number;
    public data: any
    /**
     * 
     * @param msg 错误消息
     * @param errorCode 错误编码
     */
    constructor(code?: number, msg?: string, errorCode?: number, data?: any) {
        super()
        this.code = code || 422;
        this.message = msg || "参数错误";
        this.errorCode = errorCode || 10000;
        this.data = data || []
    }
}

/**
 * 文件信息异常类
 * 
 * @author JN
 */
export class FileException extends HttpException {
    public message: string;
    public errorCode: number;
    public code: number;
    /**
     * 
     * @param msg 错误消息
     * @param errorCode 错误编码
     */
    constructor(msg: string, code?: number, errorCode?: number) {
        super()
        this.code = code || 422;
        this.message = msg || "文件名称长度错误";
        this.errorCode = errorCode || 10000;
    }
}

/**
 * 文件名称超长限制异常类
 * 
 * @author JN
 */
export class FileNameLengthLimitExceededException extends FileException {
    constructor(defaultFileNameLength: number) {
        super(`上传文件名称长度超${defaultFileNameLength}`)
    }
}

/**
 * 文件上传 误异常类
 * 
 * @author JN
 */
export class InvalidImageExtensionException extends FileException {
    public allowedExtension: Array<string>;
    public extension:string;
    public filename: string;
    constructor(allowedExtension: Array<string>, extension: string, filename: string) {
        super(`"文件[${filename}]后缀[${extension}]不正确，请上传${allowedExtension.toString()}格式"`)
        this.allowedExtension = allowedExtension;;
        this.extension = extension;
        this.filename = filename;
    }
}

/**
 * Http参数异常
 */
export class ParameterException extends HttpException {
    /**
     * 
     * @param msg 错误消息
     * @param errorCode 错误编码
     */
    constructor(msg?: string, errorCode?: number) {
        super();
        this.code = 422;
        this.message = msg || "参数错误";
        this.errorCode = errorCode || 10000;
    }
}
/**
 * http请求成功
 */
export class Success extends HttpException {
    public data: any;
    public responseType: string | undefined;
    public session: string | undefined;
    constructor(data?: any, msg = "ok", code = 200, errorCode = 0, responseType?: string, session?: string) {
        super();
        this.code = code; //200查询成功，201操作成功
        this.message = msg;
        this.errorCode = errorCode || 0;
        this.data = data;
        this.responseType = responseType;
        this.session = session;
    }
}

/**
 * http请求成功
 */
export class SuccessArg extends HttpException {
    public data: any;
    public responseType: string | undefined;
    public session: string | undefined;
    public menus: any[]
    public checkedKeys: any[]
    constructor(menus: any[], checkedKeys: any[]) {
        super();
        this.code = 200; //200查询成功，201操作成功
        this.message = "操作成功";
        this.errorCode = 0;
        this.menus = menus
        this.checkedKeys = checkedKeys
    }
}

/**
 * http请求成功
 */
export class TableSuccess extends HttpException {
    public rows: any;
    public responseType: string | undefined;
    public session: string | undefined;
    public count: number | undefined;
    constructor(rows?: any, count?: number, msg = "ok", code = 200, errorCode = 0, responseType?: string, session?: string) {
        super();
        this.count = count;
        this.rows = rows;
        this.responseType = responseType;
        this.session = session;
        this.code = code; //200查询成功，201操作成功
        this.message = msg;
        this.errorCode = errorCode || 0;
    }
}
/**
 * 返回文件流
 */
export class Buffer extends Success {
    public data: any;
    public responseType: string | undefined;
    public session: string | undefined;
    public isBuffer: boolean;
    constructor(data?: any, responseType?: string, session?: string) {
        super();
        this.code = 200; // 200查询成功，201操作成功
        this.message = "ok";
        this.errorCode = 0;
        this.data = data;
        this.responseType = responseType;
        this.session = session;
        this.isBuffer = true
    }
}
/**
 * 404
 */
export class NotFount extends HttpException {
    constructor(msg: string, errorCode: number) {
        super();
        this.code = 404;
        this.message = msg || "资源未找到";
        this.errorCode = errorCode || 10001
    }
}
/**
 * 授权失败
 */
export class AuthFailed extends HttpException {
    constructor(msg: string, errorCode?: number) {
        super();
        this.code = 401;
        this.message = msg || "授权失败";
        this.errorCode = errorCode || 10002
    }
}
/**
 * Forbbiden
 */
export class Forbbiden extends HttpException {
    constructor(msg: string, errorCode?: number) {
        super();
        this.code = 403;
        this.message = msg || "禁止访问";
        this.errorCode = errorCode || 100006;
    }
}
/**
 * 查询失败
 */
export class QueryFailed extends HttpException {
    constructor(msg?: string, errorCode?: number) {
        super();
        this.code = 500;
        this.message = msg || "未查到匹配的数据";
        this.errorCode = errorCode || 100006;
    }
}

/**
 * 系统警告
 */
export class QueryWarn extends HttpException {
    constructor(msg?: string, errorCode?: number, data?: any) {
        super();
        this.code = 601;
        this.message = msg || "系统警告";
        this.errorCode = errorCode || 100006;
        this.data == data || []
    }
}
/**
 * 数据库异常
 */
export class DataBaseFailed extends HttpException {
    constructor(msg?: string, errorCode?: number) {
        super();
        this.code = 500;
        this.message = msg || "数据库异常";
        this.errorCode = errorCode || 100005;
    }
}

/**
 * 数据库异常
 */
export class ServiceException extends HttpException {
    constructor(msg?: string, errorCode?: number) {
        super();
        this.code = 500;
        this.message = msg || "服务器异常";
        this.errorCode = errorCode || 100005;
    }
}